﻿--------------------------R3000A instructions:------------------------------------
Name:
 ADD, ADDI, ADDIU, ADDU...

Registers:
 zr, at, v0, v1, a0, a1, a2, a3, t0, t1, t2, t3, t4, t5, t6,
 t7, s0, s1, s2, s3, s4, s5, s6, s7, t8, t9, k0, k1, gp, sp, fp, ra. 

Example:
 SUB at, v0, v1

*In the generation mode, the lower register of names is allowed: add, addi, addiu, addu...
---------------------------------------------------------------------------------------------
----------------------------------Keywords:--------------------------------------------
code_start - the beginning of the generated code.(if the start code is not set, it will be equal to zero)
Example:
 const myFnStart 10b8c, myVarStart 109a0, myIndex 6f8;

 code_start 80010000;
 code_start 10000;
 code_start myFnStart;
 code_start default;

variables - the beginning of variables.( if the beginning of variables is not set, they will be written down right after instructions)
Example:
 variables 80015d14;
 variables 10afc;
 variables myVarStart;
 variables shared;

 shared - shared memory for variables in packet compilation.

file_index - Index for code insert in an executed file.(If the index is not set, the standard index will be used)
Example:
 file_index e688;
 file_index 39d90;
 file_index myIndex;
 file_index default;

The calculation of the adress code in a executable file, during compiling.(code_start - file_index)

default - for code_start is equal to 0x10000
default - for file_index is equal to 0xF800

code_limit - user limit of generated code size.
Example:
 code_limit 3ac8; the maximum size of generated code is 0x3ac8 bytes.

variables_limit - user limit on generated variables sum.
Example:
 variables_limit 1AB0; the maximum sum of generated variables is 0x1AB0 bytes.

*If the variables start is not set, and the code limit is set, the variables and code will be counted together.
*User limits are ignored if the "Insert code" flag is not set.

user_info - shows user messages during compilation, if "Show user info" parameter is checked in program.
user_info It can be useful for reminding about the critical for changes sites of code in large projects.
Example:
 user_info "My message";
---------------------------------------------------------------------------------------------
-------------------------------------Constants:----------------------------------------
Definition:
 const name value;

Example:
 const index b, index2 f;
 const index ef8a4c50;
---------------------------------------------------------------------------------------------
Definition:
 faddr name value;

Example:
 faddr func_addr 800c23e4, func_ptr 800ab436;
 faddr main_func 80010000;

*const &  faddr are synonymous.
---------------------------------------------------------------------------------------------
----------------------------------------Variables:---------------------------------------
Definition:
 byte name value;
 word name value;
 dword name value;

Example:
 const val 2;
 byte one 1, two 5, three val;
 word one 1, two 5;
 dword one 1;
---------------------------------------------------------------------------------------------
-------------------------------------------Strings:---------------------------------------
Definition:
 string name "string";

Example:
 string str1 "one";
 string str2 "two",str3 "three";
---------------------------------------------------------------------------------------------
-------------------------------------------Labels:----------------------------------------
Definition:
 name:

Example:
 label:
 here:
 exit:
---------------------------------------------------------------------------------------------
-------------------------------------------Pointer:---------------------------------------
Pointers need for writes in the executable file, the required adress.

Definition:
 get_adr pointer variable;
 get_adr pointer string;
 get_adr pointer label;

Example:
 dword pointerStr 0, pointerLable 0, pointerVar 0;
 dword var 1;
 string str "one";
 label:
 
 get_adr pointerVar var;
 get_adr pointerStr str;
 get_adr pointerLable lable;
 
 get_adr pointerVar var, pointerStr str, pointerLable lable;
 
Note:
 The pointer must be a dword variable!
---------------------------------------------------------------------------------------------
--------------------------------------Shifts:---------------------------------------------
Shifts of the received adress by the variable:

Definition:
 >>8
 <<8
 >>myShift
 <<myShift

Example:
 const myShift 4;
 get_adr pointerVar var>>8;
 get_adr pointerStr str<<2;
 get_adr pointerLable lable>>myShift;
---------------------------------------------------------------------------------------------
Getting high and low parts of the adress load/store:

Definition:
 _hi
 _hd
 _lo
 
Example:
 dword one 1; let the variable adress is equal  0x80029ca4
 LUI v0, one_hi ;v0 =80030000
 LUI v0, one_hd ;v0 =80020000
 LUI v0, one_lo ;v0 =9ca40000

Note:
 _hi - increases the high part of the adress, if the low part is more than 0x7fff.
---------------------------------------------------------------------------------------------
------------------------------------Included files:-------------------------------------
Definition:
 binc name value;

Example:
 binc my_file "C:\project\my file.bin";
 binc one "dialogs.txt", two "project\gfx.dta", three "C:\project\code.bin";

Note:
 Included files are not compile but copied.
---------------------------------------------------------------------------------------------
-----------------------------------------Patches:------------------------------------------
Commands:
 ptci - write the instruction to the specified address.
 ptcb - write byte to the specified address.
 ptcw - write 2 bytes to the specified address.
 ptcd - write 4 bytes to the specified address.
 ptcs - write string to the specified address.
 ptcm - multiple recording of the number, starting with the specified address.
 ptcf - copies the specified file to the output file.

Definition:
 ptci address instruction
 ptcb address value
 ptcs address string
 ptcm address value sum_of_copies
 ptcf address path

Example:
 ptci 80039a20 ADDI v0, v1, my_const_val
 ptcb 8002a396 10
 ptcw my_const_addr 1020
 ptcd 1ABC8 myVar
 ptcd func_addr lable
 ptcs 80010068 "patch my string"
 ptcm 80010120 FF 2
 ptcm 80010124 FFFF 3
 ptcm 8001012A 10FFFF 2
 ptcm 80010130 10AAFFFF 5
 ptcf 800101B0 "my file.bin"
 ptcf 800101B0 "C:\project\my file.bin"

* Patches are ignored if the "Insert code" flag is not checked.
---------------------------------------------------------------------------------------------
---------------------Relative jump to a constant address:-------------------
Relative jumps use constant values as the sum of instructions to jump over.
To indicate that a constant address is being used, precede it with the @ symbol.

Example:
 BEQ v0, zr, @8004a76c
 
 const my_const_addr 8003a76c;
 ptci 80027530 SKIP @my_const_addr

*The @ operator is not supported for generating to the "Info" window.
---------------------------------------------------------------------------------------------
-------------------------------Pseudo instructions:--------------------------------
LI v0, 800a8550 - load immediate.
 Result:
 v0 = 800a0000
 v0 = v0 | 8550
*If the second register is missing while loading the constant (4 bytes), the first register will be used.

LI v1, 8550
 Result:
 v1 = v1 | 8550
*If the second register is missing while loading the constant (2 bytes), the zr register will be used.

LA v0, v1, myVar - load address.
 Result:
 v1 = myVar_hi
 v0 = v1 + myVar_lo

LA v1, myVar_lo
 Result:
 v1 = v1 + myVar_lo
*If the second register is missing and used the low part _lo,  the first register will be used.

LZR v0 - load zero to register.
 v0 = 0

LONE v0 - load one to register.
 v0 = 1

LMAX v0 - load maximum value.
 v0 =FFFFFFFF

INC v0  - increment.
INCU v0 - increment unsigned.
 v0 = v0 + 1
*If the second register is missing, the first register will be used.

DEC v0 - decrement.
DECU v0 - decrement unsigned.
 v0 = v0 - 1
*If the second register is missing, the first register will be used.

COPY v0, v1 - copy second register to first.
 v0 = v1

SUBI v1, 6  - subtract immediate. 
SUBIU v1, 6 - subtract immediate unsigned.
  v1 = v1 - 6
*If the second register is missing, the first register will be used.

MULD v0, v1  - multiply on 2.
 v0 = v1 * 2
*If the second register is missing, the first register will be used.

DIVD v0, v1 - divide on 2.
 v0 = v1 / 2
*If the second register is missing, the first register will be used.

NEG v0, v1 - make a number negative.
NEGU v0, v1 - make a unsigned number negative.
 v0 = 0 - v1
*If the second register is missing, the first register will be used.

NOT v0, v1 - inversion of all bits in v1. 
 v0 = ~ v1
*If the second register is missing, the first register will be used.

GETB  v1, v0 - get low byte.
  v0 =AABBCC
  Result:
  v1 =CC
*If the second register is missing, the first register will be used.

GETW  v1, v0 - get low word.
  v0 =AABBCC
  Result:
  v1 =BBCC
*If the second register is missing, the first register will be used.

FINV v1 v0 - flag inversion.
  v0 =1
  Result:
  v1 =0
*If the second register is missing, the first register will be used.

SZB aac8(v0) - store byte equal zero.
 
SZH 0(v0) - store halfword equal zero.
 
SZW 1c(v0) - store word equal zero.

SPU - stack pointer, one step up on top.
 sp = sp - 4
SPD - stack pointer, one step down to bottom.
 sp = sp + 4
SPSU 8 - stack pointer, some steps up on top.
 sp = sp - 8
SPSD 8 - stack pointer, some steps down to bottom.
 sp = sp + 8

SREG v0, 38 - save register to stack.
 SW v0, 38(sp)

LREG v1, 38 - load register from stack.
 LW v1, 38(sp)

SRET 1c - save return address to stack.
 SW ra, 1C(sp)

LRET 1c - load return address from stack.
 LW ra, 1C(sp)

RET - return from function.
 JR ra

CALR a0 - unconditional jump on a0 and return on ra.
 JALR ra, a0

BNEZ a0, 4
 Jump if a0 is not equal zero. 

BEQZ a1, lable
  Jump if a1 is equal zero. 

SKIP 1
 Jump over the specified amount of instructions.

SKIR label
  Jump over the specified amount of instructions.(before jumping, save the return address to the ra register )

NOOP - instruction takes no action.
---------------------------------------------------------------------------------------------
------------------------------------Load and store:----------------------------------
Example of loading and storing variables:

variables 80010050;

dword var 5;
const var2 80018040;

LW v0, var(v1)
 Result:
 LUI v1, 8001
 LW v0, 0050(v1)
 
LW v0, var
 Result:
 LUI v0, 8001
 LW v0, 0050(v0)
---------------------------------------------------------------------------------------------
LW v0, var_hi(v1)
 Result:
 LW v0, 8001(v1)

LW v0, var_hd(v1)
 Result:
 LW v0, 8001(v1) 
 
LW v0, var_lo(v1)
 Result:
 LW v0, 0050(v1)
---------------------------------------------------------------------------------------------
SW v0, var2(v1)
 Result:
 LUI v1, 8002
 SW v0, 8040(v1)
 
SW v0, var2
 Result:
 LUI v0, 8002
 SW v0, 8040(v0)
---------------------------------------------------------------------------------------------
SW v0, var2_hi
 Result:
 SW v0, 8002(v0)
 
SW v0, var2_hd
 Result:
 SW v0, 8001(v0)
 
SW v0, var2_lo
 Result:
 SW v0, 8040(v0)

All load and store instructions work similarly.
---------------------------------------------------------------------------------------------
--------------------Check delay after load instructions:----------------------
After load instruction, the delay should follow.
Before using the data loaded into the register, you need to execute arbitrary instructions.
 LW v0, 8001(v1)
 ADD v1, v1, zr

 LH v0, 8001(v1)
 NOOP

Delay error:
  LB v0, 8001(v1)
  BEQ v0, zr, label

After the LWL instruction, the delay is not checked, as it can work together with LWR.
  LWL v0, 0x0003(v1)
  LWR v0, 0x0000(v1)
  NOOP
  BEQ v0, zr, label
---------------------------------------------------------------------------------------------
-------------------------------------Information:----------------------------------------
Working files by default:
 From file "asm.psig" loading instructions.
 In file "code.psx" generated machine instructions.

 *Maximum amount of strings in input file is 1500.
---------------------------------------------------------------------------------------------
-------------------------------------Comments:---------------------------------------
; - comment.
#  - comment.
--------------------------------------------------------------------------------------------
--------------------------------------Generation:--------------------------------------
[R] - reverse instruction.(for writing to file)
[F] - forward instruction.
---------------------------------------------------------------------------------------------
-------------------------------------Compilation:--------------------------------------
Compilation is made from "input file" with the text code, in the "output file" with executable code.
---------------------------------------------------------------------------------------------
Project compilation:
  The file with the "prj" extension contains the names\paths of the compiled files.
  The *.prj file should be located in the same directory as the compiled files/folders.
  The files order of compilation, is performed according to the project list.

  *All project code is loaded into the global scope.
  *The maximum amount of compiled files is 50.

The example of project, is located in the folder:
  PSIG\examples\PSIG project example\my project.prj

*You can open the file "my project.prj" with notepad.
---------------------------------------------------------------------------------------------
Packet compilation:
 The file with the "pkt" extension contains the names\paths of the compiled files.
 The *.pkt file should be located in the same directory as the compiled files/folders.
 The files order of compilation, is performed according to the packet list.

 * The files from packet can't use variables, labels, constants, each other.
 * The maximum amount of compiled files is 50.
 * Packet compilation allows use shared memory for variables.

 For using shared address of variables, you need to use the keyword "shared". 
 Example:
  variables shared;

 The address of the shared memory and the limit of variables is set in the first packet file.
 If the address of the variables is not specified in the first packet file, it will be defined
 behind the generated code of the first packet file.

The example of packet, is located in the folder:
 PSIG\examples\PSIG packet example\my packet.pkt

*The packet compilation doesn't works if the "Insert code" flag is not set.
*You can open the file "my packet.pkt" with notepad.
---------------------------------------------------------------------------------------------
The program code must be encoded in ANSI. 
---------------------------------------------------------------------------------------------
For highlight the code use the program "Notepad++".
"PSIG Highlighting syntax.xml" - file for import highlight in the Notepad++.
---------------------------------------------------------------------------------------------